﻿using System;
using System.Configuration;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using M2B.GestionEntretiens.DAL;
using M2B.GestionEntretiens.Services;
using M2B.GestionEntretiens.Contracts;

namespace M2B.GestionEntretiens.Host
{
  public class Global : System.Web.HttpApplication
  {
    IWindsorContainer _container;

    protected void Application_Start(object sender, EventArgs e)
    {
      _container = new WindsorContainer();
      //Il faut définit la chaine de connexion à deux emplacements différents 
      //1) au niveau du webConfig du projet Web (M2B.GestionEntretiens)
      //2) au nivau du webConfig du projetHost des services WCF qui hébergera les services (M2B.GestionEntretiens.Host)
      var connectionString = ConfigurationManager.ConnectionStrings["GestionEntretiens"].ConnectionString;
      _container.AddFacility<WcfFacility>()
        .Register
        (
          Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>().DependsOn(Property.ForKey("connectionString").Eq(connectionString)),
          Component.For<ICandidatsService>()
                   .ImplementedBy<CandidatsService>()
                   .Named("CandidatsService"),
          Component.For<IQuestionsService>()
                   .ImplementedBy<QuestionsService>()
                   .Named("QuestionsService"),
                   Component.For<ITestsService>()
                   .ImplementedBy<TestsService>()
                   .Named("TestsService"),
                   Component.For<IReferentielsService>()
                   .ImplementedBy<ReferentielsService>()
                   .Named("ReferentielsService"),
                   Component.For<IEntretiensService>()
                   .ImplementedBy<EntretiensService>()
                   .Named("EntretiensService"),
                   Component.For<IExamensService>()
                   .ImplementedBy<ExamensService>()
                   .Named("ExamensService")
        );
    }

    protected void Application_End(object sender, EventArgs e)
    {
      if (_container != null)
        _container.Dispose();
    }
  }
}